package com.fieldIndustry.struts2.action;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.base.Pagination.Pagination;
import com.fieldIndustry.pojo.Downloadcate;
import com.fieldIndustry.pojo.Fieldindustry;
import com.fieldIndustry.pojo.Industryfbd;
import com.fieldIndustry.service.IDownloadcateService;
import com.fieldIndustry.service.IFieldindustryService;
import com.fieldIndustry.service.IIndustryfbdService;
import com.jingyanku.pojo.Board;

public class FieldIndustryAction {
	//下载类型，行业库、图库
	private Downloadcate category;
	private IDownloadcateService downloadcateSer;
	public List<Downloadcate> categoryList = new ArrayList<Downloadcate>();
	
	//所涉及行业
	private Fieldindustry fieldIndustry;
	private IFieldindustryService fieldindustrySer;
	public List<Fieldindustry> industryList = new ArrayList<Fieldindustry>();
	
	//对应行业的算法
	private Industryfbd industryfbd;
	private IIndustryfbdService industryfbdSer;
	public List<Industryfbd> industryFbdList = new ArrayList<Industryfbd>();
	
	private Integer fieldIndustryId; // 行业下拉框ID
	HttpServletRequest request = null;
	HttpServletResponse response = null;
	private Pagination pagination; //分页工具条
	private String login;
	
	//---------------------行业库、图库等添加删除操作---------------//
	public String saveDownCate() {
		downloadcateSer.create(category);
		return listDownCate();
	}

	public String listDownCate() {
		categoryList = downloadcateSer.list("from Downloadcate", 0, Integer.MAX_VALUE, null);
		return "";
	}
	
    //------------------------------行业库行业列表-------------------------------//
	public String listIndustry() {
		categoryList = downloadcateSer.list("from Downloadcate as d where d.category ='行业库'", 0,
				Integer.MAX_VALUE, null);
		industryList = fieldindustrySer.list("from Fieldindustry", 0,
				Integer.MAX_VALUE, null);
			return "admin";
	}
	public String saveIndustry() {
		category = (Downloadcate) downloadcateSer.find(Downloadcate.class, 1);
		fieldIndustry.setCategory(category);

		copy(this.getUpload(), getImageFile());
		fieldIndustry.setFdIndustryPath(imagePath + this.getUploadFileName());
		fieldindustrySer.create(fieldIndustry);
		return listIndustry();
	}
	
	//------------------------------行业库行业算法列表---------------------------//
	public String listIndustryfbd() {
		industryFbdList = industryfbdSer.list("from Industryfbd", 0,
				Integer.MAX_VALUE, null);
			return "industry";
	}
	
	public String initAddFbd() {
		industryList = fieldindustrySer.list("from Fieldindustry", 0,
				Integer.MAX_VALUE, null);
		return "initAddFbd";
	}

	public String saveIndustryFbd() {
		 request = ServletActionContext.getRequest();
		 fieldIndustry = (Fieldindustry) fieldindustrySer.find(
				Fieldindustry.class, fieldIndustry.getFdIndustryId());
		industryfbd.setFieldIndustry(fieldIndustry);
		copy(this.getUpload(), getImageFile());
		industryfbd.setIndustryFbdPath(imagePath + this.getUploadFileName());
		industryfbdSer.create(industryfbd);
		return listIndustry();
	}

	public String loadIndustryFbd() {
		industryfbd = (Industryfbd) industryfbdSer.find(Industryfbd.class,
				industryfbd.getIndustryFbdId());
		fieldIndustry = industryfbd.getFieldIndustry();
		industryList = fieldindustrySer.list("from Fieldindustry", 0,
				Integer.MAX_VALUE, null);
		return "update";
	}

	//----------------------------------客户端显示行业库列表----------------------//
	public String industry() {
		categoryList = downloadcateSer.list("from Downloadcate as d where d.category ='行业库'", 0,
				Integer.MAX_VALUE, null);
		industryList = fieldindustrySer.list("from Fieldindustry", 0,
				Integer.MAX_VALUE, null);
		
		return "index";
	}
	
	public Downloadcate getCategory() {
		return category;
	}

	public void setCategory(Downloadcate category) {
		this.category = category;
	}


	public Fieldindustry getFieldIndustry() {
		return fieldIndustry;
	}

	public void setFieldIndustry(Fieldindustry fieldIndustry) {
		this.fieldIndustry = fieldIndustry;
	}

	public Integer getFieldIndustryId() {
		return fieldIndustryId;
	}

	public void setFieldIndustryId(Integer fieldIndustryId) {
		this.fieldIndustryId = fieldIndustryId;
	}

	public List<Fieldindustry> getIndustryList() {
		return industryList;
	}

	public void setIndustryList(List<Fieldindustry> industryList) {
		this.industryList = industryList;
	}

	public List<Industryfbd> getIndustryFbdList() {
		return industryFbdList;
	}

	public void setIndustryFbdList(List<Industryfbd> industryFbdList) {
		this.industryFbdList = industryFbdList;
	}

	public Industryfbd getIndustryfbd() {
		return industryfbd;
	}

	public void setIndustryfbd(Industryfbd industryfbd) {
		this.industryfbd = industryfbd;
	}

	public IDownloadcateService getDownloadcateSer() {
		return downloadcateSer;
	}

	public void setDownloadcateSer(IDownloadcateService downloadcateSer) {
		this.downloadcateSer = downloadcateSer;
	}

	public IFieldindustryService getFieldindustrySer() {
		return fieldindustrySer;
	}

	public void setFieldindustrySer(IFieldindustryService fieldindustrySer) {
		this.fieldindustrySer = fieldindustrySer;
	}

	public IIndustryfbdService getIndustryfbdSer() {
		return industryfbdSer;
	}

	public void setIndustryfbdSer(IIndustryfbdService industryfbdSer) {
		this.industryfbdSer = industryfbdSer;
	}
	

	public List<Downloadcate> getCategoryList() {
		return categoryList;
	}

	public void setCategoryList(List<Downloadcate> categoryList) {
		this.categoryList = categoryList;
	}


	public Pagination getPagination() {
		return pagination;
	}

	public void setPagination(Pagination pagination) {
		this.pagination = pagination;
	}

	public String getLogin() {
		return login;
	}

	public void setLogin(String login) {
		this.login = login;
	}


	private File upload;
	private String uploadFileName;
	private String uploadContentType;
	private String imagePath;


	public File getImageFile() {
		if (category != null && fieldIndustry == null && industryfbd == null) {
			imagePath = ServletActionContext.getServletContext().getRealPath(
					"/")
					+ "file/" + category.getCategory() + "/";
		}

		if (fieldIndustry != null && industryfbd == null) {
			imagePath = ServletActionContext.getServletContext().getRealPath(
					"/")
					+ "file/"
					+ fieldIndustry.getCategory().getCategory()
					+ "/"
					+ fieldIndustry.getFieldIndustry() + "/";
		}

		if (industryfbd != null) {
			imagePath = ServletActionContext.getServletContext().getRealPath(
					"/")
					+ "file/"
					+ industryfbd.getFieldIndustry().getCategory()
							.getCategory()
					+ "/"
					+ industryfbd.getFieldIndustry().getFieldIndustry() + "/";
		}
		File dir = new File(imagePath);
		if (!dir.exists()) {
			dir.mkdirs();
		}
		File imageFile = new File(dir, new String(uploadFileName));
		imageFile.deleteOnExit();
		try {
			imageFile.createNewFile();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return imageFile;
	}

	private static void copy(File src, File dst) {
		try {
			InputStream in = null;
			OutputStream out = null;
			try {
				in = new BufferedInputStream(new FileInputStream(src));
				out = new BufferedOutputStream(new FileOutputStream(dst));
				byte[] buffer = new byte[70 * 1024* 1024];
				while (in.read(buffer) > 0) {
					out.write(buffer);
				}
			} finally {
				if (null != in) {
					in.close();
				}
				if (null != out) {
					out.close();
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public File getUpload() {
		return upload;
	}

	public void setUpload(File upload) {
		this.upload = upload;
	}

	public String getUploadFileName() {
		return uploadFileName;
	}

	public void setUploadFileName(String uploadFileName) {
		this.uploadFileName = uploadFileName;
	}

	public String getUploadContentType() {
		return uploadContentType;
	}

	public void setUploadContentType(String uploadContentType) {
		this.uploadContentType = uploadContentType;
	}

	public String getImagePath() {
		return imagePath;
	}

	public void setImagePath(String imagePath) {
		this.imagePath = imagePath;
	}

}
